package com.j.lemon.learn.thread.threadLocal;

/**
 * @Author lijunjun
 * @Date 2019-01-29 15:52
 * @Description ThreadLocal 使得每一个线程都有自己的变量，但是主线程和新开的线程之间的数据不共享，也就是说子线程无法获取主线程的threadLocal值，
 * 而InheritableThreadLocal解决了这一问题，使得子线程也可以获取主线程的值。
 */
public class ThreadLocalTest {
//    public static ThreadLocalData data = new ThreadLocalData();
    public static Inher data = new Inher();
    public static void main(String[] args) throws InterruptedException {
        for(int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+ ThreadLocalTest.data.get());
        }
        Thread.sleep(1000);
        OneThread oneThread = new OneThread();
        oneThread.start();

    }
}
//class Tool{
////    public static ThreadLocalData data = new ThreadLocalData();
//    public static Inher data = new Inher();
//}

class ThreadLocalData extends ThreadLocal{
    @Override
    protected Object initialValue() {
        return System.currentTimeMillis();
    }
}

class Inher extends InheritableThreadLocal{
    @Override
    protected Object initialValue() {
        return System.currentTimeMillis();
    }
}

class OneThread extends Thread{
    @Override
    public void run() {
        for(int i =0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+ ThreadLocalTest.data.get());
        }
    }
}
